/*
 * SaveButton.java
 *
 * Created on October 12, 2004, 10:54 PM
 */

package portfolio.stock.save;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//import java.sql.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.*;

/**
 *
 * @author  ACA
 */
	/*********************************************************/
	public class SaveButton extends JButton implements ActionListener {
	    private Connection connection;
	    private String     queryText;
	    //private stockPanel mainPanelRef;
	    private JTable     table;

	    public SaveButton (String     buttonName, 
			       Connection dbConnection,
			       String     queryParam, 
			       JTable     tableParam) {

		super (buttonName);
		connection   = dbConnection;
		queryText    = queryParam;
		//mainPanelRef = mainPanelParam;
		table        = tableParam;
		this.addActionListener (this);

		return;
	    } /* End Constructor portfolioMainScr::stockPanel::SaveButton */
	    /***********************************************************/
	    public void actionPerformed(ActionEvent e) {
		if (table == null)
		    return;
		int iColNum = table.getColumnCount ();
		if (iColNum <= 0)
		    return;
		
		String sTempTable = "temp";
		dropTable (sTempTable);
		Vector headerVector = createJointTable (sTempTable);
		insertIntoTable (headerVector, sTempTable);
		
		return;
	    } /* End Method portfolioMainScr::stockPanel::SaveButton::actionPerformed */
	    /****************************************************/
	    private void dropTable (String sTableName)
	    {
		if (sTableName == null)
		    return;

		try {
		    Statement statement = connection.createStatement();
		    statement.execute ("drop table " +sTableName); //sCreate);
		}
		catch ( SQLException sqlex ) {
		    //sqlex.printStackTrace();
		    return;
		}
		return;
	    } /* End Method portfolioMainScr::stockPanel::SaveButton::dropTable */
	    /****************************************************/
  	    private Vector createJointTable (String sTableName) //String sCreate)
  	    {
		if (sTableName == null) {
		    System.err.println ("createJointTable: no table name provided.");
		    return (null);
		}

		int iColNum = table.getColumnCount ();
  		Vector headerVector = new Vector ();
  		Vector createVector = new Vector ();

		for (int i = 0; i < iColNum; i++) {
		    headerVector.addElement (table.getColumnName (i));
		    if (table.getColumnName(i).equals ("tradeDate")) {
			createVector.addElement (table.getColumnName (i)+ " date ");
		    } else if (table.getColumnName (i).equals ("week")) {
			createVector.addElement (table.getColumnName (i)+ " smallint ");
		    } else {
			createVector.addElement (table.getColumnName (i)+ " float ");
		    }
		    //System.out.println (i+" "+headerVector.get (i));
		}
		String sDBFields = createVector.toString ();
		sDBFields = sDBFields.substring (1, sDBFields.length () -1);
		String sCreateCommand = "CREATE TABLE " + sTableName + " (" + sDBFields + ");";
		System.out.println (sCreateCommand);

		try {
		    Statement statement = connection.createStatement();
		    statement.execute (sCreateCommand); //sCreate);
		}
		catch ( SQLException sqlex ) {
		    sqlex.printStackTrace();
		    return (null);
		}
		return (headerVector);
  	    } /* End Method portfolioMainScr::stockPanel::SaveButton::createJointTable */
	    /**********************************************************/
	    void insertIntoTable (Vector headerVector, String sTableName)
	    {
		if (sTableName == null) {
		    System.err.println ("insertIntoTable: no table name provided.");
		    return;
		}

		final int iTradeDatePos = 0;                    // 0j0 tradeDate field must be the first one allways
  		for (int j = 0; j < table.getRowCount (); j++) {
		    Vector valueVector = new Vector ();
  		    for (int i = 0; i < table.getColumnCount (); i++) {
			if (i == 0)
			    valueVector.addElement ("\"" + table.getValueAt (j, iTradeDatePos) + "\"");
			else 
			    valueVector.addElement (table.getValueAt (j, i));
  			//System.out.print (headerVector.get (i) +">"+table.getValueAt (j, i) +" ");
  		    }
		    String sHeader = headerVector.toString ();
		    sHeader = sHeader.substring (1, sHeader.length () -1);
		    sHeader = "insert into " + sTableName + " (" + sHeader + ") ";
		    String sValues = valueVector.toString ();
		    sValues = sValues.substring (1, sValues.length () -1);
		    sValues = "values (" + sValues + ");";
		    String sInsertCommand = sHeader + sValues;
		    //System.out.println (sInsertCommand);
		    try {
			Statement statement = connection.createStatement();
			statement.execute (sInsertCommand);
		    }
		    catch ( SQLException sqlex ) {
			sqlex.printStackTrace();
			//return (null);
		    }
  		}
	    } /* End Method portfolioMainScr::stockPanel::SaveButton::insertIntoTable */
	} /* End Class portfolioMainScr::stockPanel::SaveButton */
